<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #########################################################################################
    # Programmer : nawattapon yoskamtorn						    #
    # Email	: contact@nawattapon.com						    #
    # Website	: http://www.nawattapon.com						    #
    #########################################################################################
    
    class Contact extends CI_Controller
    {
	function __construct()
	{
	    parent::__construct();
	    if( ! $this->session->userdata('lang')){
                $this->session->set_userdata('lang','english');
            }
            
            if( ! $this->session->userdata('admin_login')){
		redirect('/admin/login/' , 'refresh');
	    }
            
	    $this->template->set_template('backend');
            $this->lang->load('admin/main', $this->session->userdata('lang'));
	    
	    /* check privilege */
	    $action = $this->uri->segment(3) ? $this->uri->segment(3) : 'view';
	    if( ! check_privilege($this->session->userdata['admin']['user_group_id'] , 'video_articles' , $action)){
		show_error( $this->lang->line('permission_denied') );
	    }
            
            $this->load->model('setting_model','setting_model');
	}
	
	public function index()
	{
	    $setting_id = array('id' => array('7','8','9','10','11'));
	    $this->setting_model->set_where_in($setting_id);
	    $data['contact_list'] = $this->setting_model->load_records();
	    
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','contact_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','contact_view',$data);
            $this->template->render();
	}
	
	public function edit($id)
	{
	    $this->setting_model->set_where(array('id'=>$id));
	    $contact_info = $this->setting_model->load_record();
	    $contact_info = json_decode($contact_info['val']);
	    
	    $data['id'] = $id;
	    $data['title_en'] = $contact_info->title->english;
	    $data['title_th'] = $contact_info->title->thai;
	    $data['email'] = $contact_info->email;
	    
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','contact_edit_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','contact_edit_view',$data);
            $this->template->render();
	}
	
	public function update()
	{
	    if (isset($_POST['id'])){
		
		$data['title'] = array('english'=>$this->input->post('title_en') , 'thai'=>$this->input->post('title_th'));
		$data['email'] = $this->input->post('email');
		$data['db'] = 'false';
		
		$input['val'] = json_encode($data);
		
		$this->setting_model->set_where(array('id'=>$this->input->post('id')));
                $this->setting_model->update_record($input);
		
                redirect('/admin/contact/' , 'refresh');
            }
	}
    }